package com.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.demo.entity.OrderEntity;
import com.demo.entity.AjaxResult;
import com.demo.entity.OrderLog;
import com.demo.entity.TbGoods;
import com.demo.fenApi.GoodsApi;
import com.demo.mapper.OrderMapper;
import com.demo.service.OrderService;
import com.demo.utils.HttpStatus;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;


@Slf4j
@Service
@RequiredArgsConstructor
public class OrderServiceImpl implements OrderService {


    @Autowired
    private GoodsApi goodsApi;

    @Resource
    private OrderMapper orderMapper;


    @Override
    @Transactional(rollbackFor = Exception.class)
    //开启seata全局事务处理
    @GlobalTransactional
    public AjaxResult saveOrder(OrderEntity tbOrder) {
        Long goodsId = tbOrder.getGoodsId();
        AjaxResult<TbGoods> tbGoodsAjaxResult = goodsApi.queryById(goodsId);
        if (tbGoodsAjaxResult.getCode() == (HttpStatus.SUCCESS)) {
            TbGoods goods = tbGoodsAjaxResult.getData();
            //商品库存足够就继续下单
            if (goods.getGoodsStock() > tbOrder.getOrderNum()) {
                //补全订单id，然后保存订单
                long orderId = IdWorker.getId();
                log.info("开始下单,订单id是{}", orderId);
                tbOrder.setOrderId(orderId);
                tbOrder.setOrderTime(new Date());
                //保存订单
                orderMapper.insert(tbOrder);
                //记录日志表   日志表 不分表
                OrderLog orderLog = new OrderLog();
                orderLog.setGoodsId(tbOrder.getGoodsId());
                orderLog.setOrderId(tbOrder.getOrderId());
                orderLog.setCreateTime(LocalDateTime.now());
                orderLog.setOrderNum(tbOrder.getOrderNum());
                goodsApi.saveLog(orderLog);
                //更新商品库存  库存表 分表
                goods.setGoodsStock(goods.getGoodsStock() - tbOrder.getOrderNum());
                goodsApi.updateStock(goods);
                //A服务操作数据 调用服务操作数据 然后返回A服务 然后抛异常
              int i = 1/0;
                return AjaxResult.success();
            } else {
                return AjaxResult.error("商品库存不足");
            }
        } else {
            return AjaxResult.error("商品服务调用失败");
        }
    }

    @Override
    public AjaxResult queryList() {
        List<OrderEntity> orderEntities = orderMapper.selectList(new QueryWrapper<>());
        return AjaxResult.success(orderEntities);
    }

    /**
     * 分片数据测试
     * @param order
     * @return
     */
    public long insert2(OrderEntity order) {
        int rs = orderMapper.saveOrder(order);
        return rs > 0 ? order.getOrderId() : 0;
    }
}
